<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="pageParams.employees">
        <el-form-item>
          <el-input v-model="queryParams.keyword" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" v-on:click="queryBykeyword" round>搜索</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" icon="el-icon-plus" @click="handleAdd" round>新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="pageParams.employees" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <!-- 编号自增长-->
      <el-table-column type="index" width="50" label="序号">
      </el-table-column>
      <el-table-column prop="username" label="员工姓名" width="120" sortable/>
      <el-table-column prop="phone" label="电话号码" width="120" sortable/>
      <el-table-column prop="email" label="邮件" width="120" sortable/>
      <el-table-column prop="age" label="年龄" width="120" sortable/>
      <el-table-column prop="department.name" label="部门名称" width="120" sortable>
      </el-table-column>
      <el-table-column label="员工状态" width="100">
        <template scope="scope">
          <span style="color: #13ce66" v-if="scope.row.state==0">启用</span>
          <span style="color: #e64242"  v-else>禁用</span>
        </template>
      </el-table-column>


      <el-table-column label="操作" >
        <template scope="scope">
          <el-button style="color: darkblue" size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button style="color: #e64242" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="queryParams.pageSize" :total="pageParams.totals" style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">

        <el-form-item label="姓名" prop="username">
          <el-input v-model="saveForm.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="saveForm.phone" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮件" prop="email">
          <el-input v-model="saveForm.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="saveForm.age" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="盐值" prop="salt">
          <el-input v-model="saveForm.salt" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="saveForm.password" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="部门名称">
          <el-select v-model="saveForm.department" value-key="id" placeholder="请选择">
            <el-option v-for="emp in departments" :label="emp.name" :value="emp">
              <span style="float: left">{{ emp.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ emp.phone }}</span>
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="登录信息" prop="logininfo_id">
          <el-input v-model="saveForm.logininfo_id" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="店铺" prop="shop_id">
          <el-input v-model="saveForm.shop_id" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="员工状态">
          <el-radio-group v-model="saveForm.state">
            <el-radio class="radio" :label="0">启用</el-radio>
            <el-radio class="radio" :label="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
// import util from '../../common/js/util'
// //import NProgress from 'nprogress'
// import { getEmployeeListPage, removeEmployee, batchRemoveEmployee, editEmployee, addEmployee } from '../../api/api';

export default {
  data() {
    return {
      pageParams:{
        employees:[],
        totals:0
      },
      queryParams:{
        pageSize: 5,
        currentPage:1,
        keyword:''
      },

      //列表忙等框
      listLoading: false,
      //批量删除 - 保存选中的列
      sels: [],//列表选中列
      title:"保存",
      departments:[],//存放部门信息
      saveFormVisible: false,//编辑界面是否显示
      saveLoading: false,
      saveFormRules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],

      },
      //编辑界面数据
      saveForm: {
        id: 0,
        username:'',
        phone: '',
        email: '',
        salt:'',
        password:'',
        age: 0,
        state: '',
        department_id: '-1',
        logininfo_id: '-1',
        shop_id: '-1',
        department:null
      },
    }
  },
  methods: {
    //性别显示转换
    formatSex: function (row, column) {
      return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
    },
    handleCurrentChange(val) {
      this.queryParams.currentPage = val;
      this.getEmployees();
    },
    queryBykeyword(){
      this.queryParams.currentPage = 1;
      this.getEmployees();
    },


    getEmployees() {
      this.listLoading=true;
      this.$http.post("/employee",this.queryParams).then(res=>{
        this.pageParams.totals=res.data.totals;
        this.pageParams.employees = res.data.data;
        this.listLoading=false;
      }).catch(res=>{
        this.$message.error("系统繁忙稍后再试")
      })
    },
    //获取部门列表
    getDept(){
      this.$http.get("/department").then(res=>{
        this.departments=res.data;
      }).catch(res=>{
        this.$message.error("查询失败")
      })
    },
    // getDeptTree(){
    //   this.$http.get("/employee/deptTree").then(res => {
    //     this.deptTree = res.data;
    //   })
    //       .catch(res => {
    //         this.$message.error('获取部门树失败,请稍后重试');
    //       })
    // }
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.$http.delete("/employee/"+row.id)
            .then(res=>{
              if (res.data.success){
                this.$message({
                  message: '删除成功',
                  type: 'success'
                });
                this.getEmployees();
              }else {
                this.$message({
                  message:res.data.message,
                  type:"error"
                });
              }
            }).catch(res=>{
          res.$message.error("系统错误");
        });
      }).catch(() => {
      });
    },

    //点击添加按钮弹出模态框
    handleAdd: function () {
      this.title = "新增";
      //重置
      this.saveForm = {
        id: null,
        phone: null,
        email: null,
        age: '',
        state: 0,
        shop_id: '',
        department:null
      };
      //显示模态框
      this.getDept();
      this.saveFormVisible = true;
    },
    //点击修改按钮弹出模态框
    handleEdit: function (index, row) {
      this.title = "修改";
      this.saveForm = Object.assign({}, row);
      this.getDept();
      this.saveFormVisible = true; //显示模态框
    },

    //编辑
    saveSubmit: function () {
      this.$refs.saveForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {})
              .then(() => {
                this.editLoading = true;
                let para = Object.assign({}, this.saveForm);
                this.$http.put("/employee",para).then(res=>{
                  if (res.data.success){
                    this.$message({
                      message: '提交成功',
                      type: 'success'
                    });
                    this.saveFormVisible = false; //关闭模态框
                    this.getEmployees();
                  }else {
                    this.$message({
                      message: res.data.msg,
                      type: 'error'
                    });
                    this.saveFormVisible = false; //关闭模态框
                  }
                }).catch(res=>{
                  this.$message.error("操作失败")
                })
              }).catch({

          })
        }
      });
    },
    //新增
    addSubmit: function () {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.addLoading = true;
            //NProgress.start();
            let para = Object.assign({}, this.addForm);
            para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
            addEmployee(para).then((res) => {
              this.addLoading = false;
              //NProgress.done();
              this.$message({
                message: '提交成功',
                type: 'success'
              });
              this.$refs['addForm'].resetFields();
              this.addFormVisible = false;
              this.getEmployees();
            });
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      let ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.patch("/employee",ids)
            .then(res=>{
              this.listLoading = false;
              if (res.data.success){
                this.$message({
                  message: '删除成功',
                  type: 'success'
                });
                this.queryParams.currentPage=1;
                this.getEmployees();
              }else {
                this.$message({
                  message: res.data.message,
                  type: 'error'
                });
              }
            })
      }).catch(() => {
      });
    }
  },
  mounted() {
    this.getEmployees();
  }
}

</script>

<style scoped>

</style>